<?php
class ModeleSessionClient extends CI_Model
{
    function __construct()
    {
        parent::__construct();
		$this->load->database();
    }
	function listeSessionClient()
	{
		$config['base_url'] = 'http://localhost/PPE/FormArmor/index.php/gestionSessionClient/lister';
		$config['total_rows'] = $this->db->get('session_form')->num_rows();
		$config['per_page'] = '4';
		
		$config['num_links'] = '3';
		$config['full_tag_open'] = '<p>';
		$config['full_tag_close'] = '</p>';

		$this->pagination->initialize($config);

		$criteres = 
			"(
				s.nb_places > s.nb_inscrits)
				and (datedebut BETWEEN now() AND NOW() + INTERVAL 2 MONTH)  
				and (s.close = 0) 
				and ('" .$this->session->userdata('matricule'). "' NOT IN(Select matricule FROM plan_formation WHERE effectue = 1))
				and (s.numero NOT IN(Select num_session FROM inscription AS i WHERE i.matricule = '" .$this->session->userdata('matricule') . "'))
				and (s.libelleform IN(Select libelle_form FROM plan_formation AS p WHERE p.matricule = '" .$this->session->userdata('matricule') . "' AND p.niveau = s.niveauform)
			)";

		$this->db->select('numero, libelleform, niveauform, datedebut, nb_places, nb_inscrits, close');
		$tableSelect = ('session_form AS s');
		$this->db->where($criteres); 
		$req = $this->db->get($tableSelect,$config['per_page'],$this->uri->segment(3));
		return $req;

	}

	function inscription($pNumSession, $pLibelle, $pNiveau)
	{
		$data = array(
		   'matricule' => $this->session->userdata('matricule'),
		   'date_inscription' => date("Y-m-d") ,
		   'num_session' => $pNumSession,
		   'etat' => 0);
		   $this->db->insert('inscription', $data); 

   		$this->db->where('numero', $pNumSession);
   		$this->db->set('nb_inscrits', 'nb_inscrits+1', FALSE);
   		$this->db->update('session_form');

   		$this->db->where('matricule', $this->session->userdata('matricule'));
   		$this->db->where('libelle_form', $pLibelle);
   		$this->db->where('niveau', $pNiveau);

   		$this->db->set('effectue', '1', FALSE);

   		$this->db->update('plan_formation');
	}
	function modificationSessionAdmin($numSession)
	{
		// critères de sélection (attention aux espaces avant et après le 1er =
		$criteres="numero = $numSession";
		$this->db->where($criteres); 
		$req = $this->db->get('session_form');
		return $req;
	}
	function suppressionSessionAdmin($numSession)
	{
		// critères de sélection (attention aux espaces avant et après le 1er =
		$criteres="numero = $numSession";
		$this->db->where($criteres);
		$req = $this->db->get('session_form');
		return $req;
	}
	/*function listeValidationAdmin()
	{
		// Chargement de la librairie pagination
		$this->load->library('pagination');
		// Chargement de la librairie table
		$this->load->library('table');
		$config['base_url'] = 'http://localhost/PPE/FormArmor/index.php/accueil/index';
		// Initialise le nombre total d'enregistrements
		$config['total_rows'] = $this->db->get('session_form')->num_rows();
		// Initialise le nombre d'enregistrements à afficher par page
		$config['per_page'] = '4';
		$config['num_links'] = '3';
		$config['full_tag_open'] = '<p>';
		$config['full_tag_close'] = '</p>';
		// Initialisation de notre pagination
		$this->pagination->initialize($config);
		// Tri de la requête
		$this->db->order_by("datedebut", "asc");
		// Exécution de la requête
		$this->db->select('numero, libelleform, niveauform, datedebut, nb_places, nb_inscrits,validation');
		$this->db->where('validation', 0);
		$req = $this->db->get('session_form',$config['per_page'],$this->uri->segment(3));
		return $req;
	}*/
}
?>